class Solution(object):
    def maxFrequency(self, nums, k):
        nums.sort()
        n = len(nums)
        diff = [0] * (n - 1)
        for i in range(n - 1):
            diff[i] = nums[i + 1] - nums[i]
        left = 0
        right = 0
        now = 0
        total = 0
        max_len = 1
        while right < n - 1:
            now += diff[right]
            total += (right - left + 1) * diff[right]
            while total > k:
                total -= now
                now -= diff[left]
                left += 1
            max_len = max(max_len, right - left + 2)
            right += 1
        return max_len

